package com.demo.juc.ticket;

/**
 * @Author: shubin_yao
 * @Date: 2022/04/30/10:24
 * @Description: 数据模型： 用多线程模拟买票，实现3个售票员卖出30张票
 * 总   结： 线程操作资源类  高内聚低耦合
 */

class Ticket {
    private int num = 30;

    public synchronized void saleTicket() {
        if (num > 0) {
            System.out.println("卖出第：" + (num--) + "张票，还剩：" + num + "张票！");
        }
    }

}


public class SaleTicket {

    public static void main(String[] args) {
        //Thread(Runnable target, String name);
        Ticket ticket = new Ticket();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.saleTicket();
                }
            }
        }, "T1");

        t1.start();

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.saleTicket();
                }
            }
        }, "T2");
        t2.start();

        Thread t3 =new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.saleTicket();
                }
            }
        }, "T3");

        t3.start();
    }
}
